package com.zpark.cloud.alibaba.controller;

import com.zpark.cloud.alibaba.service.IStorageService;
import com.zpark.cloud.alibaba.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 库存控制器
 */
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    private IStorageService storageService;

    @Value("${spring.application.name}")
    private String serverName;

    @GetMapping("/change/{pid}/{count}")
    public ResponseResult<Void> changeStorage(@PathVariable("pid") Long pid, @PathVariable("count") Integer count){
        // 调用业务层接口处理业务
        Integer row = storageService.decreaseStorage(pid, count);
        String msg = "修改库存成功";
        Integer code = 200;
        if (row <= 0) {
            msg = "修改库存失败";
            code = 500;
        }
        return new ResponseResult<>(code, serverName + msg, null);
    }
}
